﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;

namespace BitPortal.Admin.Statistic.Module
{
    public partial class StatisticAddEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                BindData(Request.QueryString["StatisticID"].ToInt64TryParse());
            }
        }
        protected void BindData(long StatisticID)
        {
            Models.Statistic st = StatisticBLL.GetByID(StatisticID);
            if (st == null) return;
            txtID.Text = StatisticID.ToString();
            txtView.Text = st.ViewCount.ToString();
            txtLikeCount.Text = st.LikeCount.ToString();
            txtDislikeCount.Text = st.DislikeCount.ToString();
            //View Ips
            if (!string.IsNullOrEmpty(st.ViewUserIPs))
            {
                var listViewIPs = from w in st.ViewUserIPs.Split('#')
                                  where !string.IsNullOrEmpty(w)
                                  select new { IP = w };
                StoreViewIPs.DataSource = listViewIPs;
                StoreViewIPs.DataBind();
            }
            //View IDs
            if (!string.IsNullOrEmpty(st.ViewUserIDs))
            {
                var listViewIDs = from w in st.ViewUserIDs.Split('#')
                                  where !string.IsNullOrEmpty(w)
                                  select new { ID = w };
                StoreViewIDs.DataSource = listViewIDs;
                StoreViewIDs.DataBind();
            }
            //Like IDs
            if (!string.IsNullOrEmpty(st.LikeUserIDs))
            {
                var listLikeIDs = from w in st.LikeUserIDs.Split('#')
                                  where !string.IsNullOrEmpty(w)
                                  select new { ID = w };
                StoreLikeIDs.DataSource = listLikeIDs;
                StoreLikeIDs.DataBind();
            }
            //Dislike IDs
            if (!string.IsNullOrEmpty(st.DislikeUserIDs))
            {
                var listDislikeIDs = from w in st.DislikeUserIDs.Split('#')
                                     where !string.IsNullOrEmpty(w)
                                     select new { ID = w };
                StoreDislikeIDs.DataSource = listDislikeIDs;
                StoreDislikeIDs.DataBind();
            }
        }
        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            long Id = Request.QueryString["StatisticID"].ToInt64TryParse();
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Models.Statistic st = bitEnt.Statistics.SingleOrDefault(ent => ent.StatisticID == Id);
            if (st == null) return;
            st.ViewCount = txtView.Text.ToInt64TryParse();
            st.LikeCount = txtLikeCount.Text.ToInt64TryParse();
            st.DislikeCount = txtDislikeCount.Text.ToInt64TryParse();
            if (bitEnt.SaveChanges() > 0)
            X.Msg.Alert("Thông báo", "Cập nhật thành công!", "parent.Ext.WindowMgr.getActive().hide();").Show();
        }
    }
}